﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.Resources;
using System.Globalization;
using System.Windows.Forms;

namespace commonV2
{
    public class language
    {
        private static myTypes.DictionaryList cultureCache = new myTypes.DictionaryList();
        public static CultureInfo GetCulture(string name)
        {
            object obj = cultureCache.Find(name);
            if (obj != null) return (CultureInfo)obj;
            CultureInfo info = new CultureInfo(name);
            cultureCache.Add(info.Name, info);
            return info;
        }

        public static CultureInfo myCulture
        {
            get { return System.Threading.Thread.CurrentThread.CurrentCulture; }
            set { System.Threading.Thread.CurrentThread.CurrentCulture = value; }
        }
        private static ResourceManager _myResourceManager = null;
        private static ResourceManager myResourceManager
        {
            get
            {
                if (_myResourceManager == null)
                    _myResourceManager = new ResourceManager("commonV2.langs.myResources", System.Reflection.Assembly.GetExecutingAssembly());
                return _myResourceManager;
            }
        }
        public static string GetString(string code)
        {
            string tmp = myResourceManager.GetString(code, language.myCulture);
            return (tmp == null ? "?" : tmp);
        }
        public static void SetLanguageForAllOpenForms(CultureInfo culture)
        {
            myCulture = culture;
            foreach (Form form in Application.OpenForms)
            {
                System.Reflection.MethodInfo method = form.GetType().GetMethod("SetLanguage");
                if (method != null) method.Invoke(form, null);
            }
        }
        public static void SetLanguage()
        {
            Settings.sysApplicationName = language.GetString("applicationName"); 
        }
    }
}